package com.example.wsm.controller;

import java.io.Console;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wsm.common.QueryPageParam;
import com.example.wsm.common.Result;
import com.example.wsm.entity.Goods;
import com.example.wsm.entity.Record;
import com.example.wsm.service.impl.GoodsService;
import com.example.wsm.service.impl.RecordService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wms
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/listPage")
    // public List<Record> listPage(@RequestBody HashMap map) {
    public Result listPageF(@RequestBody QueryPageParam query) {
        // query.get("pageSize");
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        String goodstype = (String) param.get("goodstype");
        String storage = (String) param.get("storage");
        Integer roleId = (Integer) param.get("roleId");
        Integer userid = (Integer) param.get("userid");
        Page<Record> page = new Page<Record>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(" r.goods=b.id and b.storage=c.id and b.goodsType=d.id ");
        if (roleId == 2) { 
            queryWrapper.lambda().eq(Record::getUserid, userid);
        }
        if (StringUtils.isNotBlank(name) && !"null".equals((name))) {
            // lambdaQueryWrapper.like(Record::getName, name);
            queryWrapper.like("b.name", name);
        }
        if (StringUtils.isNotBlank(storage) && !"null".equals((storage))) {
            // lambdaQueryWrapper.eq(Record::getStorage, storage);
            queryWrapper.eq("c.id", storage);
        }
        if (StringUtils.isNotBlank(goodstype) && !"null".equals((goodstype))) {
            // lambdaQueryWrapper.eq(Record::getRecordtype, Recordtype);
            queryWrapper.eq("d.id", goodstype);

        }
        IPage<Record> result = recordService.pageC(page, queryWrapper);
        return Result.suc(result.getTotal(), result.getRecords());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Record record) {
        if (record.getUserid() != ' ') {
            Goods goods = goodsService.getById(record.getGoods());
            int n = record.getCount();
            if ("2".equals(record.getAction())) {
                n = -n;
                record.setCount(n);
            }
            int num = goods.getCount() + n;
            goods.setCount(num);
            goodsService.updateById(goods);
        }

        return recordService.save(record) ? Result.suc() : Result.fail();
    }
}
